#include "draw.h"

void draw()
{
    int i;
    drawMap(map.background, map.startX, map.startY);

    drawAnimatedEntity(&player);

   for(i = 0 ; i < jeu.nombreMonstres ; i++)
    {
        drawAnimatedEntity(&monster[i]);
    }
    drawFireballs();
     drawHud();
            
    SDL_Flip(jeu.screen);
    SDL_Delay(1);
}


SDL_Surface *loadImage(char *name)
{
    SDL_Surface *temp = IMG_Load(name);
    SDL_Surface *image;

    if (temp == NULL)
    {
        printf("Failed to load image %s\n", name);
        return NULL;
    }

    SDL_SetColorKey(temp,(SDL_SRCCOLORKEY | SDL_RLEACCEL),SDL_MapRGB(temp->format,TRANS_R,TRANS_G,TRANS_B));

    image = SDL_DisplayFormat(temp);
       
    SDL_FreeSurface(temp);

    if (image == NULL)
    {
        printf("Failed to convert image %s to native format\n", name);
        return NULL;
    }

    return image;
}

void drawImage(SDL_Surface *image, int x, int y)
  {
       SDL_Rect dest;
       dest.x = x;
       dest.y = y;
       dest.w = image->w;
       dest.h = image->h;
      

       SDL_BlitSurface(image, NULL, jeu.screen, &dest);
   }

void drawStartMenu(void)
{

    char text[200];
    // TTF_Font *font = loadFont("Shut'Em Down.ttf",50);
    sprintf(text, "PROJECT ES-45");

    drawString(text, 110, 50, font);

    if(jeu.choice != 0)
    {
       // TTF_Font *font = loadFont("Shut'Em Down.ttf",50);
        sprintf(text, "START");

        drawString(text, 273, 200, font);
    }
    if(jeu.choice != 1)
    {
       // TTF_Font *font = loadFont("Shut'Em Down.ttf",50);
        sprintf(text, "QUIT");
        drawString(text, 273, 280, font);
    }

    if(jeu.choice == 0)
    {
       // TTF_Font *font = loadFont("Shut'Em Down.ttf",50);
        sprintf(text, "> START");
        drawString(text, 250, 200, font);
    }
    else if(jeu.choice == 1)
    {
       // TTF_Font *font = loadFont("Shut'Em Down.ttf",50);
        sprintf(text, "> QUIT");
        drawString(text, 250, 280, font);
    }
//TTF_Font *font = loadFont("Shut'Em Down.ttf",50);
    sprintf(text, "PROJECT ES-45 ");
    drawString(text, 120, 420, font);


}
void drawHud(void)
{
    
    char text[200];


  
    drawImage(jeu.HUD_vie,600,20);
    
    sprintf(text, "%d", jeu.vies);
   
    drawString(text, 560, 20, font);
   // drawImage(jeu.HUD_etoiles, 60, 60);
    sprintf(text, "%d ", jeu.etoiles);
    
   
    drawString(text,20, 20, font);
   
}
void updateStartMenu()
{
   
    if(input.down == 1)
    {
       
        if(jeu.choice == 0)
            jeu.choice++;

        input.down = 0;
    }

    if(input.up == 1)
    {
        if(jeu.choice == 1)
            jeu.choice--;

        input.up = 0;
    }

    if(input.enter)
    {
         if(jeu.choice == 0)
        {
            //jeu.level = 1;
            initializePlayer();
            //changeLevel();
            jeu.vies = 3;
            jeu.etoiles = 0;
            jeu.onMenu = 0;
        }
    
        else if(jeu.choice == 1)
        {
            exit(0);
        }
         input.enter = 0;
    }
   

}   
void drawPauseMenu(void)
{
     char text[200];   

    sprintf(text, "** PAUSE **");
    drawString(text, 240, 240, font);

}     

void updatePauseMenu()
{
    
    if(input.enter)
    {
        jeu.onMenu = 0;
        input.enter = 0;
    }

}



void delay(unsigned int frameLimit)
{
    unsigned int ticks = SDL_GetTicks();

    if (frameLimit < ticks)
    {
        return;
    }

    if (frameLimit > ticks + 16)
    {
        SDL_Delay(16);
    }
    else
    {
        SDL_Delay(frameLimit - ticks);
    }
}